<template>
  <el-dialog title="新增部门" :visible="dialogShow">
    <el-form :rules="rules" :model="form" label-width="100px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" placeholder="1-50个字符"></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" placeholder="1-50个字符"></el-input>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择">
          <el-option
          v-for="(item,index) in list" 
          :key="index"
          :label="item.username"
          :value="item.username"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" placeholder="1-300个字符"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button>取 消</el-button>
      <el-button type="primary">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDeptApi } from '@/api/departments'
import { getSimpleList } from '@/api/employees'
export default {
  props: ['dialogShow', 'id'],
  data () {
    return {
      list: [],
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, massage: '长度 1-50 个字符', trigger: 'blur' },
          {
            validator: async (rule, value, callback) => {
              const res = await getDeptApi()
            //   console.log(res)
              // const res1 = res.data.depts.filter(item=>{
              //     return item.pid === this.id
              // })
              // const bool = res1.some(item=>{
              //     return item.name === value
              // })
              // bool ? callback(new Error('同级部门中禁止出现重复部门')) : callback()
              res.data.depts.some(
                item => item.pid === this.id && item.name === value
              )
                ? callback(new Error('同级部门中禁止出现重复部门'))
                : callback()
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, massage: '长度 1-50 个字符', trigger: 'blur' },
          {
            validator: async (rule, value, callback) => {
              const res = await getDeptApi()
              res.data.depts.some(item => item.code === value)
                ? callback(new Error('部门编码不能重复'))
                : callback()
            },
            trigger: 'blur'
          }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, massage: '长度 1-300 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  async created () {
    const res = await getSimpleList()
    console.log(res);
    this.list = res.data
  }
}
</script>

<style></style>
